package com.ruoyi.demowechatpay.service.impl;


import com.ruoyi.demowechatpay.enumeration.TransferDetailStatusEnum;
import com.ruoyi.demowechatpay.pojo.dto.*;
import com.ruoyi.demowechatpay.pojo.vo.MyBatchesQueryResult;
import com.ruoyi.demowechatpay.pojo.vo.MyTransferCreateResult;
import com.ruoyi.demowechatpay.service.BusinessNumberManager;
import com.ruoyi.demowechatpay.service.MyMerchantTransferServiceImpl;
import com.ruoyi.demowechatpay.service.TransferService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import java.util.Arrays;
import java.util.Objects;

@Slf4j
@Service
@RequiredArgsConstructor
public class TransferServiceImpl implements TransferService {

    private final MyMerchantTransferServiceImpl myMerchantTransferService;
    private final BusinessNumberManager businessNumberManager;

    @Override
    public MyTransferCreateResult transferBills(WeChatTransferDTO transferDTO) {
        MyTransferCreateRequest request = buildTransferRequest(transferDTO);
        log.info("微信转账到零钱请求参数：{}", request);
        try {
            return myMerchantTransferService.transferBills(request);
        } catch (Exception e) {
            log.error("微信转账到零钱调用失败: {}", e.getMessage(), e);
            throw new RuntimeException("微信转账到零钱调用失败", e);
        }
    }

    @Override
    public TransferDetailStatusEnum queryTransfer(WeChatTransferQueryDTO dto) {
        MyWxBatchesQueryRequest queryRequest = MyWxBatchesQueryRequest.builder()
                .transferBillNo(dto.getTransferBillNo())
                .build();
        log.info("查询微信转账到零钱请求参数：{}", queryRequest);
        try {
            MyBatchesQueryResult result = myMerchantTransferService.queryTransferBillNo(queryRequest);
            return Objects.nonNull(result) ?
                    TransferDetailStatusEnum.valueOf(result.getState()) :
                    TransferDetailStatusEnum.FAIL;
        } catch (Exception e) {
            log.error("查询微信转账到零钱调用失败: {}", e.getMessage(), e);
            throw new RuntimeException("查询微信转账到零钱调用失败", e);
        }
    }

    // 封装请求对象构建逻辑
    private MyTransferCreateRequest buildTransferRequest(WeChatTransferDTO transferDTO) {
        return MyTransferCreateRequest.builder()
                .appid(transferDTO.getAppId())
                .openid(transferDTO.getOpenid())
                .userName(transferDTO.getUserName())
                .outBillNo(businessNumberManager.generateShortV2("BILL"))
                .transferAmount(transferDTO.getTransferAmount())
                .transferRemark(transferDTO.getBatchRemark())
                .transferSceneId("1005")
                .transferSceneReportInfos(Arrays.asList(
                        createReportInfo("岗位类型", "修车师傅"),
                        createReportInfo("报酬说明", "用户本月佣金")
                ))
                .build();
    }

    private MyTransferCreateRequest.TransferSceneReportInfo createReportInfo(String type, String content) {
        MyTransferCreateRequest.TransferSceneReportInfo info = new MyTransferCreateRequest.TransferSceneReportInfo();
        info.setInfoType(type);
        info.setInfoContent(content);
        return info;
    }
}